home *** CD-ROM | disk | FTP | other *** search
- /* released under the GNU public license, see www.gnu.org */
-
- #pragma pack(2)
-
- #include <Common.h>
- #include <System/SysAll.h>
- #include <UI/UIAll.h>
- #include <ExgMgr.h>
- #include <System/FileStream.h>
- #include "boxer.h"
-
- #include <Unix/sys_types.h>
-
- #include "stringil.h"
- #include "stdio2.h"
-
- /* this is the callback stuff from Callback.h by ian in O'Reilly's book */
- register void *reg_a4 asm("%a4");
-
- #define CALLBACK_PROLOGUE \
- void *save_a4 = reg_a4; asm("move.l %%a5,%%a4; sub.l #edata,%%a4" : :)
-
- #define CALLBACK_EPILOGUE reg_a4 = save_a4
-
- static int toprow = 0;
- static int maxrow = 0;
- static int selection = -1;
-
- static int mode;
-
- static char *filelist;
- static char filecard[64];
- static long int filedisp[64];
- FileHand archfd;
-
-
- /* **************************************************************************** */
-
- static void TableDrawCell(VoidPtr tableP, Word row, Word column,
- RectanglePtr bounds)
- {
- char *c;
- c = (char *) TblGetRowData(tableP, row);
- WinDrawChars(c, strlen(c), bounds->topLeft.x, bounds->topLeft.y);
- }
-
- /* **************************************************************************** */
-
- int unzipdir(FileHand infile, char *dir, long int *loc);
- int untardir(FileHand fd, char *dir, long int *loc);
-
- static void scandbs()
- {
- LocalID lid;
- UInt16 cardno, attr;
- UInt32 type, crea;
- int i;
- char tp[40], *c;
-
- maxrow = 0;
- if (filelist == NULL)
- filelist = MemPtrNew(1024);
-
- c = filelist;
- *c = 0;
- c[1] = 0;
- maxrow = 0;
- for (cardno = 0; cardno < MemNumCards(); cardno++) {
- for (i = 0; i < DmNumDatabases(cardno); i++) {
- lid = DmGetDatabase(cardno, i);
- DmDatabaseInfo(cardno, lid, tp, &attr, NULL, NULL, NULL, NULL, NULL, NULL,
- NULL, &type, &crea);
- if (!(attr & dmHdrAttrStream) || type != 'DATA')
- continue;
- if (crea != 'NZIP' && crea != 'BRWS' && crea != 'ATCH' && crea != 'BOXR')
- continue;
-
- strcpy(c, tp);
- filecard[maxrow] = cardno;
- filedisp[maxrow++] = i;
-
- c += strlen(c) + 1;
- *c = 0;
-
- }
- }
- }
-
-
-
- int unzip(FileHand infile, long int loc);
- int untar(FileHand fd, long int loc);
- int MakeDoc(char *name, FileHand fd, int destruct);
- int MakeDatabase(FileHand fd, int destruct);
-
- static Boolean TableEventHandler(EventPtr event)
- {
- TablePtr tptr;
- Word n;
- FormPtr frm;
- int handled = false;
- UInt top;
- Word row, numRows;
- char *c;
- LocalID lid;
- FileHand fd;
- char name[256];
- UInt32 len, type, crea;
-
- switch (event->eType) {
- case frmOpenEvent:
-
- WinEraseWindow();
- frm = FrmGetActiveForm();
- tptr = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, maintable));
- top = toprow;
- numRows = TblGetNumberOfRows(tptr);
-
- c = filelist;
- n = top;
- while (n-- && *c)
- c += strlen(c) + 1;
-
- for (row = 0; row < numRows; row++, top++) {
-
- if (*c) {
- TblSetRowData(tptr, row, (ULong) c);
- c += strlen(c) + 1;
- }
-
- if (top < maxrow) {
- for (n = 0; n < 1; n++) {
- TblSetItemStyle(tptr, row, n, customTableItem);
- TblSetCustomDrawProcedure(tptr, n, TableDrawCell);
- }
- TblMarkRowInvalid(tptr, row);
- } else
- TblSetRowUsable(tptr, row, false);
- }
- for (n = 0; n < 1; n++)
- TblSetColumnUsable(tptr, n, true);
-
- FrmDrawForm(frm);
-
- selection = -1;
- break;
-
- case keyDownEvent:
- if (event->data.keyDown.chr != pageUpChr &&
- event->data.keyDown.chr != pageDownChr) break;
-
- frm = FrmGetActiveForm();
- tptr = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, maintable));
- n = TblGetNumberOfRows(tptr);
- toprow += n * (((event->data.keyDown.chr == pageDownChr) << 1) - 1);
- if ((toprow + n) >= maxrow)
- toprow = maxrow - n;
- if (toprow < 0)
- toprow = 0;
-
- FrmGotoForm(FrmGetActiveFormID());
-
- handled = true;
- break;
-
- case ctlSelectEvent:
- {
- int eid;
-
- handled = true;
-
- eid = event->data.ctlEnter.controlID;
-
- if (eid == bexit) {
- mode = 0;
- FileClose(archfd);
- scandbs();
- FrmGotoForm(mainform);
- break;
- }
-
- if (selection == -1)
- break;
-
- switch (eid) {
-
- case bunzip:
-
- if (mode) {
- WinEraseWindow();
- unzip(archfd, filedisp[selection]);
- FrmGotoForm(unzipform);
- break;
- }
-
- lid = DmGetDatabase(filecard[selection], filedisp[selection]);
- DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &type, &crea);
- archfd = FileOpen(filecard[selection], name, type, crea,
- fileModeUpdate | fileModeAnyTypeCreator, NULL);
- n = unzipdir(archfd, filelist, filedisp);
-
- if (n) {
- mode = 1;
- maxrow = n;
- FrmGotoForm(unzipform);
- } else {
- FrmAlert(4000);
- FileClose(archfd);
- scandbs();
- FrmGotoForm(mainform);
- }
-
-
- break;
-
- case buntar:
-
- if (mode) {
- WinEraseWindow();
- untar(archfd, filedisp[selection]);
- FrmGotoForm(untarform);
- break;
- }
-
- lid = DmGetDatabase(filecard[selection], filedisp[selection]);
- DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &type, &crea);
- archfd = FileOpen(filecard[selection], name, type, crea,
- fileModeUpdate | fileModeAnyTypeCreator, NULL);
- n = untardir(archfd, filelist, filedisp);
- if (n) {
- mode = 2;
- maxrow = n;
- FrmGotoForm(untarform);
- } else {
- FrmAlert(4000);
- FileClose(archfd);
- scandbs();
- FrmGotoForm(mainform);
- }
-
- break;
-
-
- case bripunzip:
-
- lid = DmGetDatabase(filecard[selection], filedisp[selection]);
-
- DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &type, &crea);
-
- fd = FileOpen(filecard[selection], name, type, crea,
- fileModeUpdate | fileModeAnyTypeCreator, NULL);
-
- n = unzip(fd, -1);
- if (n == -1) {
- FileSeek(fd, 0, fileOriginBeginning);
- n = untar(fd, -1);
- }
- FileClose(fd);
- if (n != -1)
- FileDelete(filecard[selection], name);
-
- scandbs();
- break;
-
- case btodoc:
-
- lid = DmGetDatabase(filecard[selection], filedisp[selection]);
- DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &type, &crea);
- fd = FileOpen(filecard[selection], name, type, crea,
- fileModeUpdate | fileModeAnyTypeCreator, NULL);
-
- strcat(name, ".box");
-
- {
- int destr;
- destr = FrmAlert(3000);
- MakeDoc(name, fd, destr);
- FileClose(fd);
- if (destr) {
- name[strlen(name) - 4] = 0;
- FileDelete(filecard[selection], name);
-
- }
-
- }
-
- scandbs();
- FrmGotoForm(mainform);
-
- break;
-
- case binstall:
-
- lid = DmGetDatabase(filecard[selection], filedisp[selection]);
- DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &type, &crea);
- fd = FileOpen(filecard[selection], name, type, crea,
- fileModeUpdate | fileModeAnyTypeCreator, NULL);
- {
- int destr;
- destr = FrmAlert(3000);
- MakeDatabase(fd, destr);
- FileClose(fd);
- if (destr)
- FileDelete(filecard[selection], name);
- }
-
- scandbs();
- FrmGotoForm(mainform);
-
- break;
-
- case bdelete:
-
- if (FrmAlert(2000))
- break;
-
- lid = DmGetDatabase(filecard[selection], filedisp[selection]);
- DmDeleteDatabase(filecard[selection], lid);
- scandbs();
- FrmGotoForm(mainform);
- break;
-
- case bbeam:
-
- {
- Word err;
- UInt32 xcnt, outhis, tlen;
- ExgSocketType exgSocket;
- char name[256],tname[64];
-
- lid = DmGetDatabase(filecard[selection], filedisp[selection]);
-
- DmDatabaseInfo(filecard[selection], lid, name, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &type, &crea);
-
-
- fd = FileOpen(filecard[selection], name, type, crea,
- fileModeReadOnly | fileModeAnyTypeCreator, NULL);
- FileTell(fd, &len, NULL);
-
- MemSet(&exgSocket, sizeof(exgSocket), 0);
- strcpy(tname,name);
- exgSocket.description = tname;
- exgSocket.name = tname;
- exgSocket.length = len;
- exgSocket.target = 'BOXR';
-
- err = ExgPut(&exgSocket);
- if (err)
- break;
- xcnt = 0;
- tlen = 0;
- while (!err && xcnt < len) {
- if( xcnt + tlen < len ) {
- outhis = FileRead(fd, &name[tlen], 1, 256 - tlen, NULL);
- tlen += outhis;
- }
- outhis = ExgSend(&exgSocket, name, tlen, &err);
- if (err)
- break;
- xcnt += outhis;
- tlen -= outhis;
- }
- ExgDisconnect(&exgSocket, err);
- FileClose(fd);
- }
-
- break;
- }
- }
- break;
-
- case tblSelectEvent:
- selection = toprow + event->data.tblSelect.row;
- #if 0
- {
- char buf[20];
- UInt32 type, crea;
-
- StrIToA(buf, selection);
- strcat(buf, " ");
- WinDrawChars(buf, strlen(buf), 0, 145);
- lid = DmGetDatabase(filecard[selection], filedisp[selection]);
- DmDatabaseInfo(filecard[selection], lid, NULL, NULL, NULL, NULL, NULL,
- NULL, NULL, NULL, NULL, &type, &crea);
- WinDrawChars(&type, 4, 40, 145);
- WinDrawChars(&crea, 4, 80, 145);
- }
- #endif
- handled = true;
- break;
- default:
- return 0;
- }
- return handled;
- }
-
- /* **************************************************************************** */
-
- int dirtyp;
-
- DWord PilotMain(Word cmd, Ptr cmdPBP, Word launchFlags)
- {
- short err;
- int formID;
- FormPtr form;
- EventType event;
-
- if (cmd == sysAppLaunchCmdExgReceiveData) {
- ExgSocketPtr exgSocketP = (ExgSocketPtr) cmdPBP;
- unsigned char buf[256];
- int inthis;
- FileHand fd;
-
- err = ExgAccept(exgSocketP);
-
- fd = FileOpen(0, exgSocketP->name, 'DATA', 'BOXR',
- fileModeReadWrite | fileModeAnyTypeCreator, NULL);
-
- if ((launchFlags & sysAppLaunchFlagSubCall)) {
- CALLBACK_PROLOGUE;
- dirtyp++;
- CALLBACK_EPILOGUE;
- }
-
- if (err)
- return err;
-
- inthis = 1;
- while (inthis) {
- inthis = ExgReceive(exgSocketP, buf, 256, &err);
- if (err)
- break;
- FileWrite(fd, buf, 1, inthis, NULL);
- }
- ExgDisconnect(exgSocketP, err);
- FileClose(fd);
- return 0;
-
- } else if (cmd == sysAppLaunchCmdExgAskUser) {
- ExgAskParamPtr exgP = (ExgAskParamPtr) cmdPBP;
- exgP->result = exgAskDialog; /* Dialog, Ok, Cancel */
- return 0;
- } else if( cmd != sysAppLaunchCmdNormalLaunch )
- return 0;
-
- toprow = 0;
- dirtyp = 1;
- filelist = NULL;
- do {
- if (dirtyp) {
- scandbs();
- FrmGotoForm(mainform);
- dirtyp = 0;
- }
-
- EvtGetEvent(&event, 0);
- if (SysHandleEvent(&event))
- continue;
- if (MenuHandleEvent((void *) 0, &event, &err))
- continue;
- if (event.eType == frmLoadEvent) {
- formID = event.data.frmLoad.formID;
- form = FrmInitForm(formID);
- FrmSetActiveForm(form);
- switch (formID) {
- case mainform:
- case unzipform:
- case untarform:
- FrmSetEventHandler(form, TableEventHandler);
- break;
- }
- }
- FrmDispatchEvent(&event);
- } while (event.eType != appStopEvent);
- return 0;
- }
-